package com.jzqf.harmonydemo.views;

import com.jzqf.harmonydemo.utils.PermissionUtil;
import com.jzqf.harmonydemo.views.slice.ListContainerAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.bundle.IBundleManager;
import ohos.security.SystemPermission;


public class ListContainerAbility extends Ability {
    private final int PERMISSION_REQUEST_CODE = 102;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(ListContainerAbilitySlice.class.getName());
        requestPermission();
    }

    /**
     * 动态获取权限
     */
    private void requestPermission() {
        PermissionUtil.request(this, SystemPermission.WRITE_USER_STORAGE,
                SystemPermission.CAMERA).subscribe(aBoolean -> {

        });
    }


    @Override
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults);
        if (requestCode == PERMISSION_REQUEST_CODE) {
            if (grantResults.length > 0
                    && grantResults[0] == IBundleManager.PERMISSION_GRANTED) {
                // 权限被授予
                // 注意：因时间差导致接口权限检查时有无权限，所以对那些因无权限而抛异常的接口进行异常捕获处理
            } else {
                // 权限被拒绝
                terminateAbility();
            }
        }
    }
}
